Brief Full Advanced Search Search Tips
|  |  | To access the contents, click the chapter and section titles. Visual Basic 6 Programming Blue Book: The Most Complete, Hands-On Resource for Writing Programs with Microsoft Visual Basic 6! (Publisher: The Coriolis Group) Author(s): Peter G. Aitken ISBN: 1576102815 Publication Date: 08/01/98 Search this book:
Previous | Table of Contents | Next
To define a structure that contains another structure as an element, follow the same syntax. The definition of the embedded structure—in this case, StockItem—must come first in the code. Then:
    Type OrderData
       Part As StockItem
       Supplier As String * 30
    End Type
To access elements of the embedded structure, use the period syntax as follows:
    Dim LastOrder As OrderData
    LastOrder.Part.Name = “cam tensioning spring”
    LastOrder.Part.Cost = 99.76
You must define structures at the module level. In other words, you cannot include a Type...End Type statement inside a procedure. No special rules exist for using the elements of structures in your program; you can use them anywhere you could use a simple variable of the same type. If your project includes a definition of a user-defined type, the auto list of data types that Visual Basic displays while you are typing a Dim statement will include the type. You’ll learn about using arrays of user-defined types and arrays in user-defined types later in the chapter. #### Arrays An array stores a large number of variables under the same name. All the individual vari-ables, or elements, of an array must be the same data type. These elements are distinguished from each other by a numerical array index. You create an array with the Dim statement, as shown here:
    Dim Data(100) As Integer
This statement declares an array containing 101 type Integer elements. Why 101 rather than 100? In Visual Basic, array indexes begin with 0, so this array starts at element Data(0) and ends at Data(100), for a total of 101 elements. An array can be any of Visual Basic’s data types, including both variable- and fixed-length strings. You can also create an array of user-defined types that you have defined with the Type...End Type statement. If the data type is not specified in the Dim statement, the array defaults to Variant. You can use the elements of an array anywhere you can use regular variables of the same type. The real power of arrays becomes evident when you use variables as the array index, or subscript. In fact, any numerical expression that evaluates to a value within the array’s index range can be used as the subscript. For example, here’s a For...Next loop that fills all the elements of a 1,000-element array with the values 1 through 1,000:
    For i = 0 To 999
        Array(i) = i + 1
    Next I
Visual Basic array indexes start at zero by default, but you can modify this. To set the lower index to 1 for all arrays, include the Option Base1 statement at the module level before any array declarations. After executing Option Base 1, the declaration
    Dim Array(100) As Integer
creates an array with 100 elements numbered 1 through 100. A more flexible way to modify the lower index is with the To keyword. You use To in the array declaration statement:
    Dim Array1(1 To 10)    ‘ 10 elements 1 through 10
    Dim Array2(-5 To 5)    ‘ 11 elements -5 through 5
Arrays can have more than one index; these are called multidimensional arrays. To create a multidimensional array, include the size of each dimension in the declaration statement. The statement
    Dim Array(9, 9)
Declares an array with 100 elements, Array(0, 0) … Array(0, 9) … Array(9, 9). You can have as many as 26 dimensions in an array, although it is rare to need more than 3 or 4 even for the most specialized applications. If you try to reference a nonexistent array element, Visual Basic generates a “Subscript out of range” error. For example, if you declare an array as follows:
    Dim Array(10)
then the statement
    Array(11) = 5
will generate the error, because the array does not have an element 11. You can create arrays of user-defined structures, and you can also include an array as an element of a structure. To create an array of structures, you must first define the structure with the Type...End Type statement, as shown here:
    Type Person
        FirstName As String * 15
        LastName As String * 20
    End Type
Then, you use the Dim statement to create instances of the structure. For example, the line
    Dim MyFriends(100) As Person
will create an array of 100 type Person structures. You access the elements of the structures within the array by using a combination of the structure’s period notation (covered earlier in this chapter in the section “User-Defined Data Types”) and the array subscript notation. Thus, the code
    MyFriends(n).FirstName = “Bill”
    MyFriends(n).LastName = “Gates”
would place data in both the FirstName and LastName elements of the nth structure in the array. To use an array as an element of a structure, include it in the structure definition as follows:
    Type Person
        FirstName As String * 15
        LastName As String * 20
        Likes(10) As String * 20
    End Type
If you create a single instance of the structure, you can access the elements of the array element by using array subscript notation on the right side of the period:
    Dim MyBestFriend As Person
    MyBestFriend.FirstName = “Pat”
    MyBestFriend.LastName = “Smith”
    MyBestFriend.Likes(1) = “chocolate”
    MyBestFriend.Likes(2) = “old movies”
Declaring an array of a structure that includes an array as an element complicates mat-ters. For example, consider this code (assuming the definition of type Person shown previously):
    Dim MyFriends(100) As Person
    MyFriends(1).FirstName = “Pat”
    MyFriends(1).LastName = “Smith”
    MyFriends(1).Likes(1) = “chocolate”
    MyFriends(1).Likes(2) = “old movies”
All you need to remember is that the subscript for an array of structures goes to the left of the period, and the subscript for an array within a structure goes to the right of the period. Dynamic Arrays The arrays I have discussed so far are known as static arrays. Their size—number of dimensions and number of elements—is set when the array is declared and cannot be changed. Another type of array, called dynamic, does not have these limitations. When you first declare a dynamic array, you specify its data type, but that’s all. For example, the following statement declares a dynamic array of type Integer:
    Dim MyArray() As Integer
Then, when you know the required size of the array, use ReDim to specify it. Here are some examples:
    ReDim MyArray(100)
    ReDim MyArray(10,10,20)
    ReDim MyArray (1 to 100)
Once you have applied ReDim, you can use the array like any static array. Even more flexibility is provided by the fact that you can ReDim a dynamic array as many times as needed, changing the number of dimensions and/or elements to suit the program’s needs. ReDim is permitted only in procedure-level code. Dynamic arrays follow the same scope rules as static arrays. In other words, if you declare the array at the module level with Dim, its scope is limited to that module. To make the dynamic array visible outside the module, use the Public keyword:
    Public MyArray() As Integer
Of course, if you declare a dynamic array at the procedure level its scope is local to that procedure.
Previous | Table of Contents | Next

| Products | Contact Us | About Us | Privacy | Ad Info | Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.